package com.iwhalecloud.bss.kite.cucc.service.custview.evolve;

import java.util.Objects;
import java.util.concurrent.Callable;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.vo.custview.CustViewBaseVO;
import com.iwhalecloud.bss.kite.cucc.service.custview.evolve.task.AbstractCustViewTask;

import com.ztesoft.bss.common.bo.LoginInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.zsmart.core.log.ZSmartLogger;

/**
 * @ClassName : CustViewCallable
 * @Description :
 * @Author : xie.zhengjun
 * @Date : 2021/4/30 16:28
 */
public class CustViewCallable implements Callable<CustViewBaseVO> {
    private final ZSmartLogger LOGGER = ZSmartLogger.getLogger(CustViewCallable.class);

    private String sessionId;

    private LoginInfo loginInfo;

    private Object[] args;

    private AbstractCustViewTask custViewTask;

    @Override
    public CustViewBaseVO call() {
        try {
            if (Objects.isNull(custViewTask)) {
                return null;
            }
            LOGGER.info(custViewTask.getClass().getSimpleName() + ">>>>>>>>>>>>>>>>>>开始执行任务>>>>>>>>>>>>>");
            long start1 = System.currentTimeMillis();
            if (KiteStringUtils.isNotEmpty(sessionId) && Objects.nonNull(loginInfo)) {
                ContextUtil.setHttpSessoinId(sessionId);
                ContextUtil.setLoginInfo2LoginInfoThreadLocal(loginInfo);
            }

            CustViewBaseVO custView = custViewTask.call(args);

            long end1 = System.currentTimeMillis();
            LOGGER.info(custViewTask.getClass().getSimpleName() + ">>>>执行结束>>>>" + "总耗时" + (end1 - start1) + "毫秒");
            return custView;
        }
        catch (Exception e) {
            LOGGER.error(custViewTask.getClass().getSimpleName() + "执行失败", e);
            return null;
        }
        finally {
            ContextUtil.removeThreadLocal();
        }
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public LoginInfo getLoginInfo() {
        return loginInfo;
    }

    public void setLoginInfo(LoginInfo loginInfo) {
        this.loginInfo = loginInfo;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public AbstractCustViewTask getCustViewTask() {
        return custViewTask;
    }

    public void setCustViewTask(AbstractCustViewTask custViewTask) {
        this.custViewTask = custViewTask;
    }
}
